package conceptmaniax.hotelapp.util.image;

import java.io.File;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.WindowManager;
import conceptmaniax.hotelapp.config.Constants;

public final class ImageUtils {

	private ImageUtils() {
		super();
	}
	
	public static float screenDeviceRatio(final float widthPx) {
		return widthPx/320.0f;
	}
	
	public static float screenDeviceRatio(final float widthPx, final float heightPx) {
		return widthPx/heightPx;
	}
	
	public static float aspectRatio(final float deviceRatio, final float bitmapwidth, final float bitmapheight) {
		return deviceRatio* (bitmapwidth/bitmapheight);
	}

	public static float aspectRatio(final float deviceRatio, final int bitmapwidth, final int bitmapheight) {
		float aspectRatio = 0.0f;
		try {
			aspectRatio = deviceRatio * (bitmapwidth / bitmapheight);
		} catch (ArithmeticException ae) { }
		
		return aspectRatio;
	}
	
	public static float ImageRatioI(final float bitmapwidth, final float bitmapheight) {
		return (bitmapheight/bitmapwidth);
	}
	
	public static BitmapFactory.Options getOptionsFromResource(final Context context, int imageId) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(context.getResources(), imageId, options);
		return options;
	}
	
	public static BitmapFactory.Options getOptionsFromImagesFile(final Context context, String photo) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(context.getFilesDir().getAbsolutePath() + File.separator + Constants.IMAGES_FOLDER + File.separator + photo, options);
		return options;
	}

	public static int getScale(int originalWidth, int originalHeight, final int requiredWidth, final int requiredHeight) {
		int inSampleSize = 1;
		if ((originalWidth > requiredWidth) || (originalHeight > requiredHeight)) {
			if (originalWidth < originalHeight) {
				inSampleSize = Math.round((float) originalWidth / requiredWidth);
			} else {
				inSampleSize = Math.round((float) originalHeight / requiredHeight);
			}
		}

		return inSampleSize;
	}
	
	public static int getScaleForWidth(Context context, int viewWidth){
	    int deviceWidth = getDeviceWidth(context); 
	    Double val = Double.valueOf(deviceWidth)/Double.valueOf(viewWidth);
	    val = val * 100d;
	    return val.intValue();
	}
	
	public static int getDeviceWidth(Context context) {
		WindowManager mWinMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		return mWinMgr.getDefaultDisplay().getWidth();
	}
	
	public static int getDeviceHeigth(Context context) {
		WindowManager mWinMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		return mWinMgr.getDefaultDisplay().getHeight();
	}
}
